package com.caifeng.myapplication.expandableList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.caifeng.myapplication.R;

import java.util.List;

public class GroupBeanAdapter extends BaseExpandableListAdapter {
    private List<GroupBean> mList;
    private LayoutInflater inflater;


    public GroupBeanAdapter(List<GroupBean> mList, Context context) {
        this.mList = mList;
        this.inflater = LayoutInflater.from(context);

    }


    @Override
    public int getGroupCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mList.get(groupPosition).getmList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mList.get(groupPosition).getmList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    public class ViewHolderGroup {
        ImageView imageViewIconIndicator;
        TextView gr_name, gr_online;

    }

    public class ViewHolderChild {
        ImageView ch_pic;
        TextView ch_name, ch_online, ch_text;

    }

    //
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View v, ViewGroup parent) {
        ViewHolderGroup viewHolderGroup = null;
        if (v == null) {
            viewHolderGroup = new ViewHolderGroup();
            v = inflater.inflate(R.layout.ex_group, parent, false);
            viewHolderGroup.imageViewIconIndicator = v.findViewById(R.id.img);
            viewHolderGroup.gr_name = v.findViewById(R.id.gr_title);
            viewHolderGroup.gr_online = v.findViewById(R.id.gr_online);
            v.setTag(viewHolderGroup);
        } else {
            viewHolderGroup = (ViewHolderGroup) v.getTag();

        }
        GroupBean groupBean = mList.get(groupPosition);
        if (isExpanded) {
            viewHolderGroup.imageViewIconIndicator.setImageResource(R.mipmap.ic_launcher);
        } else {
            viewHolderGroup.imageViewIconIndicator.setImageResource(R.mipmap.ic_launcher_round);
        }
        viewHolderGroup.gr_name.setText(groupBean.getGr_title());
        viewHolderGroup.gr_online.setText(groupBean.getGr_online() + "/" + groupBean.getGr_totalPerson());


        return v;
    }

    //

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View v, ViewGroup parent) {
        ViewHolderChild viewHolderChild = null;
        if (v == null) {
            viewHolderChild = new ViewHolderChild();
            v = inflater.inflate(R.layout.ex_child, parent, false);
            GroupBean.ChildBean childBean=
                    mList.get(groupPosition).getmList().get(childPosition);
            viewHolderChild.ch_name = v.findViewById(R.id.ch_name);
            viewHolderChild.ch_online = v.findViewById(R.id.ch_online);
            viewHolderChild.ch_pic = v.findViewById(R.id.ch_pic);
            viewHolderChild.ch_text = v.findViewById(R.id.ch_text);
            v.setTag(viewHolderChild);
        } else {
            viewHolderChild = (ViewHolderChild) v.getTag();

        }
        GroupBean.ChildBean childBean =
                mList.get(groupPosition).getmList().get(childPosition);
        viewHolderChild.ch_text.setText(childBean.getText());
        viewHolderChild.ch_pic.setImageResource(childBean.getAcatarid());
        viewHolderChild.ch_name.setText(childBean.getName());
        viewHolderChild.ch_online.setText(childBean.getOnline());


        return v;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }


}
